html = '''
<div id="container">
<ul class="list">
<li class = "item-0">first item</li>
<li class = "item-1"><a href = "link2.html">second item</a></li>
<li class = "item-0 active"><a href = "link3.html"><span class="bold">third item</span></a></li>
<li class = "item-1" active><a href = "link4.html">fourth item</a></li>
<li class = "item-0"><a href = "link5.html">fif item</a>
</ul>
</div>
'''
# 基本css选择器
from pyquery import PyQuery as pq
doc = pq(html)
print(doc('#container .list li'))#先选取id为container的节点，然后在选取它内部的class为list节点内部的所有li节点
print(type(doc('#container .list li')))
# 查找节点
items = doc('.list')
print(type(items))
print(items)
lis = items.find('li')#所有子孙节点
print(type(lis))
print(lis)
lis = items.children()#所有子节点
print(type(lis))
print(lis)
lis = items.children('.active')#class为active的节点
print(lis)

html='''
<div class=<wrap">
<div id="container">
<ul class="list">
<li class = "item-0">first item</li>
<li class = "item-1"><a href = "link2.html">second item</a></li>
<li class = "item-0 active"><a href = "link3.html"><span class="bold">third item</span></a></li>
<li class = "item-1" active><a href = "link4.html">fourth item</a></li>
<li class = "item-0"><a href = "link5.html">fif item</a>
</ul>
</div>
</div>
'''
doc = pq(html)
items = doc('.list')
container = items.parent()#直接父节点
print(type(container))
print(container)
print('*******************')
parents = items.parents()#祖先节点
print(type(parents))
print(parents)
# 正确答案
# <div class="wrap">
# <div id="container">
# <ul class="list">
# <li class="item-0">first item</li>
# <li class="item-1"><a href="link2.html">second item</a></li>
# <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
# <li class="item-1" active=""><a href="link4.html">fourth item</a></li>
# <li class="item-0"><a href="link5.html">fif item</a>
# </li></ul>
# </div>
# </div>
# <div id="container">
# <ul class="list">
# <li class="item-0">first item</li>
# <li class="item-1"><a href="link2.html">second item</a></li>
# <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
# <li class="item-1" active=""><a href="link4.html">fourth item</a></li>
# <li class="item-0"><a href="link5.html">fif item</a>
# </li></ul>
# </div>
print('*******************')
parent = items.parents('.wrap')
print(parent)
# 正确答案
# <div class="wrap">
# <div id="container">
# <ul class="list">
# <li class="item-0">first item</li>
# <li class="item-1"><a href="link2.html">second item</a></li>
# <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
# <li class="item-1" active=""><a href="link4.html">fourth item</a></li>
# <li class="item-0"><a href="link5.html">fif item</a>
# </li></ul>
# </div>
# </div>